// pages/tweet/publish/at/at.js
import {
  FOLLOWS_LIST,
  SEARCH,
} from "../../../../request/api";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    keyword: '',
    selectedCount: 0,
    selectedList: [],
    searchResultList: [],
    myFollowList: [],
    localList: [],
    myFollowPageToken: '',
    searchPageToken: '',
    userId: '',
    showLoadMoreFollows: true,
    showLoadMoreSearch: true,
    loadingFollows: false,
    loadingSearch: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.userId = getApp().globalData.userId;
    this.loadFollows();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  confirmSelect: function(){
    if(this.data.selectedCount == 0){
      wx.navigateBack();
      return;
    }
    var pages = getCurrentPages();
    var prePage = pages[pages.length - 2];
    prePage.setData({
      selectedUsers: this.data.selectedList,
      reloadByAt: true,
    });
    wx.navigateBack();
  },

  inputChange: function (e) {
    var key = e.detail.value;
    var list = this.data.myFollowList;
    var tempLocalList = [];
    list.forEach(element => {
      if(element.name.indexOf(key) >= 0){
        tempLocalList.push(element);
      }
    });
    this.setData({
      keyword: key,
      localList: tempLocalList,
      searchResultList: [],
      searchPageToken: [],
      showLoadMoreSearch: true,
    });
  },

  clearInput: function () {
    this.setData({
      keyword: '',
    });
  },

  itemClick: function (e) {
    var type = e.currentTarget.dataset.type;
    var index = e.currentTarget.dataset.index;
    switch (type) {
      case 1:
        var list = this.data.myFollowList;
        var user = list[index];
        this.updateCurrentClickList(user);
        this.setData({
          myFollowList: list,
        });
        break;
      case 2:
        var list = this.data.localList;
        var user = list[index];
        this.updateCurrentClickList(user);
        var tempFollowList = this.data.myFollowList;
        this.setData({
          localList: list,
          myFollowList: tempFollowList,
        });
        break;
      case 3:
          var list = this.data.searchResultList;
          var user = list[index];
          this.updateCurrentClickList(user);
          this.setData({
            searchResultList: list,
          });
          break;
      case 4:
        var list = this.data.selectedList;
        var user = list[index];
        this.updateSelectList(user, false);
        var tempFollowList = this.data.myFollowList;
        var tempLocalList = this.data.localList;
        var tempSearchList = this.data.searchResultList;
        this.updateOtherListByClickSelectListItem(tempFollowList, user);
        this.updateOtherListByClickSelectListItem(tempLocalList, user);
        this.updateOtherListByClickSelectListItem(tempSearchList, user);
        this.setData({
          myFollowList: tempFollowList,
          localList: tempLocalList,
          searchResultList: tempSearchList,
        });
        break;
    }
  },

  updateOtherListByClickSelectListItem: function(list, user){
    var length = list.length;
      for (let index = 0; index < length; index++) {
        const element = list[index];
        if(user.id == element.id){
          element.checked = false;
          break;
        }
      }
  },

  updateOtherListByClickListItem: function(list, user, check){
    var length = list.length;
      for (let index = 0; index < length; index++) {
        const element = list[index];
        if(user.id == element.id){
          element.checked = check;
          break;
        }
      }
  },

  updateCurrentClickList: function(user){
    if(user.checked){
      user.checked = false;
      this.updateSelectList(user, false);
    } else {
      if(this.checkSelectedListLimit()){
        user.checked = true;
        this.updateSelectList(user, true);
      }
    }
  },

  updateSelectList: function(user, add){
    var list = this.data.selectedList;
    if(add){
      list.push(user);
    } else {
      var length = list.length;
      for (let index = 0; index < length; index++) {
        const element = list[index];
        if(user.id == element.id){
          list.splice(index, 1);
          break;
        }
      }
    }
    this.setData({
      selectedList: list,
      selectedCount: list.length,
    });
  },

  checkSelectedListLimit: function(){
    if(this.data.selectedCount < 10){
      return true;
    } else {
      wx.showToast({
        title: '最多选择10个',
      });
      return false;
    }
  },

  refreshFollows: function(){
    this.data.myFollowPageToken = '';
    this.data.myFollowList = '';
    this.loadFollows();
  },

  loadFollows: function () {
    if(this.data.loadingFollows){
      return;
    }
    this.data.loadingFollows = true;
    FOLLOWS_LIST({
      'id': this.data.userId,
      'pageToken': this.data.myFollowPageToken
    }).then(res => {
      this.loadingFollows = false;
      if (res.data.code == 1) {
        var tempList = this.data.dataList;
        var result = res.data.result;
        if (this.data.myFollowPageToken.length == 0) {
          tempList = result.items
        } else {
          tempList = tempList.concat(result.items)
        }
        this.data.myFollowPageToken = result.nextPageToken

        this.setData({
          myFollowList: tempList,
          showLoadMoreFollows: result.requestCount <= result.responseCount,
        });
      }
    }).catch(err => {
      this.data.loadingFollows = false;
    });
  },

  refreshSearch: function(){
    this.data.searchPageToken = '';
    this.data.searchResultList = [];
    this.loadMoreSearch();
  },
  loadMoreSearch: function(){
    if(this.data.loadingSearch){
      return;
    }
    this.data.loadingSearch = true;
    SEARCH({
      'content': this.data.keyword,
      'pageToken': this.data.searchPageToken,
      'catalog': 11
    }).then(res => {
      this.data.loadingSearch = false;
      if (res.data.code == 1) {
        var tempList = this.data.searchResultList;
        var result = res.data.result;
        if (this.data.searchPageToken.length == 0) {
          tempList = result.items
        } else {
          tempList = tempList.concat(result.items)
        }
        this.data.searchPageToken = result.nextPageToken

        this.setData({
          searchResultList: tempList,
          showLoadMoreSearch: result.requestCount <= result.responseCount,
        });
      }
    }).catch(err => {
      this.data.loadingSearch = false;
    });
  },
})